﻿using System;
using Scala.Data;

namespace Scala.Logic
{
    public abstract class EntityBaseInt : IEntity<int>
    {

        #region IEntity<int> Members

        public virtual int Id { get; protected set; }

        public EntityBaseInt()
        {
        }

        #endregion

        #region Equatable Members
        public override bool Equals(object obj)
        {
            return Equals(obj as EntityBaseInt);
        }
        private static bool IsTransient(EntityBaseInt obj)
        {
            return obj != null &&
            Equals(obj.Id, default(int));
        }
        private Type GetUnproxiedType()
        {
            return GetType();
        }
        public virtual bool Equals(EntityBaseInt other)
        {
            if (other == null)
                return false;
            if (ReferenceEquals(this, other))
                return true;
            if (!IsTransient(this) &&
            !IsTransient(other) &&
            Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return thisType.IsAssignableFrom(otherType) ||
                otherType.IsAssignableFrom(thisType);
            }
            return false;
        }
        public override int GetHashCode()
        {
            {
                if (Equals(Id, default(int)))
                    return base.GetHashCode();
                return Id.GetHashCode();
            }
        }

        #endregion
    }
}
